package ibmetp.mis.dao;


import ibmetp.mis.vo.Bulletin;
import ibmetp.mis.vo.Page;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class BulletinDao  extends BaseDao{
	/**
	 * 将查询结果转换成bulletin类集，并保存进List集合中
	 * @author  ZHENG BAOFA
	 * @create 2011/10/8
	 */
	public List ResultSet2List(ResultSet rs) {
		List<Bulletin> resultList = new ArrayList<Bulletin>();
		try {
			while (rs != null && rs.next()) {
				Bulletin bulletin = new Bulletin();
				bulletin.setBulletinId(rs.getLong("BULLETIN_ID"));
				bulletin.setPublishMsg(rs.getString("PUBLISH_MSG"));
				bulletin.setPrincipal(rs.getString("PR_ID"));
				bulletin.setCreateDate(rs.getTimestamp("CREATE_DATE"));
				resultList.add(bulletin);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultList;
	}
	public Bulletin getBulletinById(Long id) {
		String sql = "select * from MSG_T_BULLETINt where t.BULLETIN_ID=?";
 
		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;
		Bulletin bulletin =null;// 
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			ResultSet rs = ps.executeQuery();

			while (rs != null && rs.next()) {
				bulletin=new Bulletin();
				bulletin.setBulletinId(rs.getLong("BULLETIN_ID"));
				bulletin.setPublishMsg(rs.getString("PUBLISH_MSG"));
				bulletin.setPrincipal(rs.getString("PR_ID"));
				bulletin.setCreateDate(rs.getTimestamp("CREATE_DATE"));
				
				

			}
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bulletin;

	}
    
	
	
	/**
	 * 通过公告Id更新公告信息
	 * @param bulletin
	 */
	public void updateBulletin(Bulletin bulletin)
    {
    	String sql="update MSG_T_BULLETIN set PUBLISH_MSG=?,PR_ID=?,UPDATE_DATE=sysdate where BULLETIN_ID=?";
 
    	
    	System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;

		try {
			
			ps = conn.prepareStatement(sql);
			
			ps.setString(1,bulletin.getPublishMsg());
			ps.setString(2,bulletin.getPrincipal());
			ps.setLong(3, bulletin.getBulletinId());
			
			
			ps.execute();
			//ResultSet rs = ps.executeQuery();
			
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    } 
	
	/**
	 * 添加公告消息
	 * 
	 * @param publishMsg
	 */
	
	public void addPublishMsg(Bulletin publishMsg) {
		String sql="insert into MSG_T_BULLETIN (BULLETIN_ID,CREATE_DATE,PUBLISH_MSG,PR_ID) values (BULLETIN_SEQ.NEXTVAL,sysdate,?,?) ";

		
		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps=null;

		try {
			ps = conn.prepareStatement(sql);
//			ps.setLong(1, publishMsg.getBulletinId());
			ps.setString(1,publishMsg.getPublishMsg());
			ps.setString(2,publishMsg.getPrincipal());
			
			//ps.setLong(5,user.getDept().getDeptId());
			ResultSet rs = ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				ps.close();
				conn.close();
			} 
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 根据公告Id删除公告信息
	 * 
	 * @param id
	 */
	public void deleteById(Long id) {

		String sql = "delete from MSG_T_BULLETIN t where t.BULLETIN_ID=?";

		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;

		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			ResultSet rs = ps.executeQuery();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 批量删除公告
	 * @param bulletinIds
	 */
	public void deleteByIds(String bulletinIds)
	{
		String sql = "delete from MSG_T_BULLETIN t where t.BULLETIN_ID IN ("+bulletinIds+")";
		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;

		try {
			ps = conn.prepareStatement(sql);
			//ps.setString(1, userIds);
			ResultSet rs = ps.executeQuery();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
   
	/**
	 * 查询用户信息
	 * @param pageNo 当前页面
	 * @param pageSize页面大小
	 * @param bulletin 公告条件
	 * @return
	 */
	public Page getBulletinPage(int pageNo, int pageSize, Object bulletin) {

		return this.find(bulletin, pageNo, pageSize,"(SELECT * FROM MSG_T_BULLETIN order by CREATE_DATE desc)");

	}
	
	
	/**
	 * 根据公告ID获取公告信息
	 * @param BULLETIN_ID
	 * @return
	 */
	public Bulletin getBulletinById(String bulletinId)
	{
		String sql = "select * from MSG_T_BULLETIN t where t.BULLETIN_ID=?";

		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;
		Bulletin bulletin =null;// 
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, bulletinId);
			ResultSet rs = ps.executeQuery();

			while (rs != null && rs.next()) {
				bulletin =new  Bulletin();
				
				bulletin.setBulletinId(rs.getLong("BULLETIN_ID"));
				bulletin.setPublishMsg(rs.getString("PUBLISH_MSG"));
				bulletin.setPrincipal(rs.getString("PR_ID"));
				

			}
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bulletin;
	}
	
	/**
	 * 添加查询条件
	 */
	public Map<Integer, Object[]> getCondition(Object o) {
		Map<Integer, Object[]> paramsMap = new HashMap<Integer, Object[]>();
		Integer count = 0;
		Bulletin bulletin = (Bulletin) o;
		
		if (bulletin.getPublishMsg() != null
				&& bulletin.getPublishMsg().trim().length() > 0) {
			count++;
			paramsMap.put(count, new Object[] { " and PUBLISH_MSG like ? ",
					"%" + bulletin.getPublishMsg() + "%" });
		}
		return paramsMap;
	}





	


	
	
}
